package com.rao.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * rabbitmq 入门之生产者（直接型运用）
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2021/6/17 21:35
 */
public class ProducerTest {

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂（设置访问账号与密码，默认用户密码：guest）
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("admin");
        factory.setPassword("admin");
        //设置 RabbitMQ 地址
        factory.setHost("192.168.235.102");
        //建立到代理服务器到连接
        Connection conn = factory.newConnection();
        //获得信道
        Channel channel = conn.createChannel();
        //声明交换器
        String exchangeName = "hello-exchange";
        channel.exchangeDeclare(exchangeName, "direct", true);
        String routingKey = "hello";
        //发布消息
        byte[] messageBodyBytes = "quit".getBytes();
        channel.basicPublish(exchangeName, routingKey, null, messageBodyBytes);
        System.out.println("已发布消息");
        channel.close();
        conn.close();
    }

}
